package cn.com.egova.materialdesignexample.activity;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.squareup.picasso.Picasso;
import org.json.JSONObject;
import java.util.List;
import cn.com.egova.materialdesignexample.R;
import cn.com.egova.materialdesignexample.cardview.Meizi;
import cn.com.egova.materialdesignexample.cardview.MyOkhttp;

public class CardViewActivity extends AppCompatActivity {
    private RecyclerView mRecyclerView;
    private RecyclerView.LayoutManager mLayoutManager;
    private RecyclerView.Adapter mAdapter;
    private ProgressBar pb;
    private List<Meizi> meizis;
    private String URL = "http://gank.io/api/data/福利/10/1";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_card_view);

        initToolbar();

        initView();
    }

    private void initToolbar() {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("CardView");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationIcon(R.mipmap.icon_back);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    private void initView() {
        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        mLayoutManager = new GridLayoutManager(this, 4, GridLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(mLayoutManager);
        pb = (ProgressBar) findViewById(R.id.pb);

        new GetData().execute();
    }


    class CardViewAdapter extends RecyclerView.Adapter<CardViewAdapter.ViewHolder> {
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_card_view, parent, false);
            ViewHolder holder = new ViewHolder(view);
            return holder;
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            if(meizis!=null && meizis.size()>0){
                Meizi meizi = meizis.get(position);
                if(meizi!=null && !TextUtils.isEmpty(meizi.getUrl())){
                    //加载图片并填充到ImageView.
                    Picasso.with(CardViewActivity.this).load(meizi.getUrl()).into(holder.iv);
                }
            }
        }

        @Override
        public int getItemCount() {
            return meizis.size();
        }

        class ViewHolder extends RecyclerView.ViewHolder {
            private ImageView iv;

            public ViewHolder(View itemView) {
                super(itemView);
                this.iv = (ImageView) itemView.findViewById(R.id.iv);
            }
        }
    }


    private class GetData extends AsyncTask<String, Integer, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            pb.setVisibility(View.VISIBLE); //显示进度条.
        }

        @Override
        protected String doInBackground(String... params) {

            return MyOkhttp.get(URL);
        }

        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            pb.setVisibility(View.GONE); //隐藏进度条.

            if (!TextUtils.isEmpty(result)) {
                JSONObject jsonObject;
                Gson gson = new Gson();
                String jsonData = null;

                try {
                    jsonObject = new JSONObject(result);    //实际上这个JSONObject就是在当map在玩.
                    jsonData = jsonObject.getString("results");
                    //看url的返回结果{
                    // "error": false,
                    //     "results": [
                    //      {
                    if (meizis == null || meizis.size() == 0) {
                        meizis = gson.fromJson(jsonData, new TypeToken<List<Meizi>>() {}.getType());
                        mRecyclerView.setAdapter(mAdapter = new CardViewAdapter());
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
